/**
 * Author: Shen Yanqiu
 * Date: 2017-12-09
 * Time: 15:34
 *
 */
import { routerRedux } from "dva/router";
import modelExtend from 'dva-model-extend';
import { pageModel } from '../common';
import { message } from 'antd';
import { getCode, doforgetPassword } from "../../services/forgetPassword";

export default modelExtend(pageModel, {
  namespace: 'forgetPassword',

  state: {
  },

  subscriptions: {
  },

  effects: {
    // 修改密码获取验证码
    * getCode ({ payload: params }, { call, put }) {
      params.type = 'ForgetPassword';
      const code = yield call(getCode, params);
      if (code.success) {
        message.success("验证码发送成功！");
      }
    },
    // 执行确认修改
    * doforgetPassword ({ payload: params }, { call, put }) {
      console.log(params);
      const resultInfo = yield call(doforgetPassword, params);
      if (!resultInfo.success) {
        console.log(resultInfo);
        resultInfo.prompt ?
          message.error(resultInfo.prompt) :
          message.error("修改密码失败，请检查输入是否正确！");
        return;
      }
      // console.log(resultInfo);
      message.success("修改密码成功，请登录！");
      yield put(
        routerRedux.push({ pathname: '/login' })
      );
    },
    * doChangePassword ({ payload: params }, { call, put }) {
      const resultInfo = yield call(doforgetPassword, params);
      if (!resultInfo.success) {
        console.log(resultInfo);
        resultInfo.prompt ?
          message.error(resultInfo.prompt) :
          message.error("修改密码失败，请检查输入是否正确！");

        return;
      }
      // console.log(resultInfo);
      message.success("修改密码成功");
    },
  },

  reducers: {
    update (state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
});
